package robotinterface.list.actions;

import java.awt.Component;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import robotinterface.list.*;
import robotinterface.list.entry.*;

/**
 *
 * @author Brian Bailey
 */
public class ListContextMenu extends MouseAdapter {

    RobotList list;

    public ListContextMenu(RobotList list) {
        this.list = list;
    }

    private void showContextMenu(Component c, int x, int y) {
        Border border = UIManager.getBorder("List.focusCellHighlightBorder");
        JPopupMenu contextMenu = new JPopupMenu();

        JMenuItem addItem = new JMenuItem("Add");
        addItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RobotListModel model = (RobotListModel) list.getModel();
                int mouseIndex = list.locationToIndex(list.getMousePosition());
                int lastIndex = model.getSize();

                if ((lastIndex <= mouseIndex + 1)) {
                    model.add(new Entry("Edit Me!"));
                } else {
                    model.addElementAt(mouseIndex, new Entry("Edit Me!"));
                }
            }
        });

        JMenuItem deleteItem = new JMenuItem("Delete");
        deleteItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RobotListModel model = (RobotListModel) list.getModel();
                if (!list.isSelectionEmpty()) {
                    int index = list.getSelectedIndex();
                    model.removeElementAt(index);
                }
            }
        });

//        JMenu executeMenu = new JMenu("Execute");
//        JMenuItem runItem = new JMenuItem("Restart");
//        runItem.addActionListener(new ActionListener()
//		{
//            @Override
//			public void actionPerformed(ActionEvent e)
//			{
//                int mouseIndex = list.locationToIndex( list.getMousePosition());
//                list.init( true, true, 0 );
//			}
//		});

//        JMenuItem forwardItem = new JMenuItem("Forwards");
//        forwardItem.addActionListener(new ActionListener()
//		{
//            @Override
//			public void actionPerformed(ActionEvent e)
//			{
//                int mouseIndex = list.locationToIndex( list.getMousePosition());
//                list.init( false, true, mouseIndex );
//                list.start();
//			}
//		});

//        JMenuItem backwardItem = new JMenuItem("Backwards");
//        backwardItem.addActionListener(new ActionListener()
//		{
//            @Override
//			public void actionPerformed(ActionEvent e)
//			{
//               int mouseIndex = list.locationToIndex( list.getMousePosition());
//               list.init( false, false, mouseIndex );
//               list.start();
//			}
//		});

//        executeMenu.add( runItem );
//        executeMenu.addSeparator();
//        executeMenu.add( forwardItem );
//        executeMenu.add( backwardItem );

        contextMenu.add(addItem);
        contextMenu.add(deleteItem);
        contextMenu.addSeparator();
//        contextMenu.add(executeMenu);

        contextMenu.show(c, x, y);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.isPopupTrigger()) {
            showContextMenu(e.getComponent(), e.getX(), e.getY());
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger()) {
            showContextMenu(e.getComponent(), e.getX(), e.getY());
        }
    }
}
